﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Dynamic;
using Newtonsoft.Json.Linq;
using System.Web.Script.Services;
using System.Runtime.Remoting;
using System.Reflection.Emit;





public static class MyClass
{

    public static List<dynamic> ToDataList<TSource>(this DataTable dataTable)
    {
        dynamic _objlist = new List<dynamic>();
        foreach (DataRow dataRow in dataTable.Rows)
        {
            Dictionary<string, object> myDict = new Dictionary<string, object>();
            dynamic ob = new ExpandoObject();
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                myDict.Add(Convert.ToString(dataTable.Columns[i]), dataRow[i]);
            }

            var data = GetDynamicObject(myDict);
            _objlist.Add(data);
        }




        return _objlist;
    }


    public static dynamic GetDynamicObject(Dictionary<string, object> properties)
    {
        return new MyDynObject(properties);
    }

    public sealed class MyDynObject : DynamicObject
    {
        private readonly Dictionary<string, object> _properties;

        public MyDynObject(Dictionary<string, object> properties)
        {
            _properties = properties;
        }

        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return _properties.Keys;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (_properties.ContainsKey(binder.Name))
            {
                result = _properties[binder.Name];
                return true;
            }
            else
            {
                result = null;
                return false;
            }
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (_properties.ContainsKey(binder.Name))
            {
                _properties[binder.Name] = value;
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public static List<TSource> ToList<TSource>(this DataTable dataTable) where TSource : new()
    {
        var dataList = new List<TSource>();

        const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
        var objFieldNames = (from PropertyInfo aProp in typeof(TSource).GetProperties(flags)
                             select new
                             {
                                 Name = aProp.Name,
                                 Type = Nullable.GetUnderlyingType(aProp.PropertyType) ?? aProp.PropertyType
                             }).ToList();
        var dataTblFieldNames = (from DataColumn aHeader in dataTable.Columns
                                 select new { Name = aHeader.ColumnName, Type = aHeader.DataType }).ToList();
        var commonFields = objFieldNames.Intersect(dataTblFieldNames).ToList();

        foreach (DataRow dataRow in dataTable.AsEnumerable().ToList())
        {
            var aTSource = new TSource();
            foreach (var aField in commonFields)
            {
                PropertyInfo propertyInfos = aTSource.GetType().GetProperty(aField.Name);
                propertyInfos.SetValue(aTSource, dataRow[aField.Name], null);
            }
            dataList.Add(aTSource);
        }
        return dataList;
    }

}

[System.Web.Script.Services.ScriptService]
public class DataService : System.Web.Services.WebService
{

    DatabaseClass _objDatabaseClass = new DatabaseClass();
    [WebMethod]
    public RegistrationData[] Bind_AluminiPics(dynamic data)
    {
        List<DatabaseFields> listDB = new List<DatabaseFields> {
          new DatabaseFields{ FieldName = "@DeptID",FieldType=DbType.Int16,FieldValue=data["SelectedStream"]},
          new DatabaseFields{ FieldName = "@PassingYear",FieldType=DbType.Int16,FieldValue=data["PassingYear"]}
        };

        return _objDatabaseClass.SelectDataset_ProcName("ALM.Select_AllAlumini", listDB).Tables[0].ToList<RegistrationData>().ToArray(); 
    }

    RegistrationData _objRegistrationData = new RegistrationData();
    [WebMethod(EnableSession = true)]
    public string Deactivate_AdImageVideos(int AdImageVideoID, int AdID)
    {
        string Message = string.Empty;
        if (!object.Equals(Session["User"], null))
        {
            _objRegistrationData = Session["User"] as RegistrationData;
            DAL.AdDB _objAdDB = new DAL.AdDB();
            if (_objAdDB.CheckAd_IsPostedByUser(AdID, _objRegistrationData.UserID))
                _objAdDB.Deactivate_AdImageVideos(AdImageVideoID);
            else Message = "You are not authorized to delete this image.";
        }
        else
        {
            Message = "You need to be login to delete this image.";
        }
        return Message;
    }

    [WebMethod] 
    public StreamDetails[] GetAllStreams()
    {
        return _objDatabaseClass.SelectDataset_ProcName("NTS.Select_Streams").Tables[0].ToList<StreamDetails>().ToArray();
    }
}
